$(function(){
	var service = new EbeimService();
    var jring = $("#j-ring");
    var statics=$("#statics");
    var days=$("#days");
    var todayCount=$("#todayCount");
    var allCount=$("#allCount");
    var user = plugin.cookieToJson(document.cookie);
    var deg = 0;
    var data1, data2;
    var jusername = $("#j-username"),
    	jrole = $("#j-role"),
    	jpointer = $("#j-pointer"),
    	jmemWave = $("#j-memWave"),
    	jpushMessage = $("#j-pushMessage");
    
    jusername.html(user.username);
    if(user.role == "SYSADMIN"){
    	jrole.html("系统管理员");
    }else if(user.role == "ADMIN"){
    	jrole.html("管理员");
    }else{
    	jrole.html("普通用户");
    }
    service.getFirstPageStatics({userId:user.userId},function(json){
		if(json.code == 200) {
			statics.text(json.statics.usageSize+"G/"+json.statics.countSize+"G");
			var i = json.serverInfo.endTime.indexOf(" ");
			days.text(json.serverInfo.endTime.substring(0,i));
			todayCount.text(json.backuptimes.todayCount);
			allCount.text(json.backuptimes.allCount);
			data1 = {
		        color:"#FFAE00",
		        text:"",
		        value:(json.statics.usageSize/json.statics.countSize)*100,
		    }
		    data2 = {
		        color:"#0DE6E9",
		        text:"",
		        value:((365*json.ca.authTime-json.serverInfo.days)/365*json.ca.authTime)*100,
		    }
			ringChart.diagram("space-chart",data1);
		    ringChart.diagram("time-chart",data2);
		}else{
			plugin.msgtip({type: "i",msg: json.desc});
        }
	});
    setInterval(function(){
        if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE8.0") {
            var dx = dx=-239/2*Math.cos(deg)+235/2*Math.sin(deg)+239/2,
                dy=-239/2*Math.sin(deg)-235/2*Math.cos(deg)+235/2,
                m11= Math.cos(deg),
                m12= -Math.sin(deg),
                m21= Math.sin(deg),
                m22= Math.cos(deg);
            jring.css("filter","progid:DXImageTransform.Microsoft.Matrix(Dx="+dx+",Dy="+dy+",M11="+m11+",M12="+m12+",M21="+m21+",M22="+m22+");")
            deg++;
            if(deg == 360){
                deg = 0;
            }
        }else{
            jring.css("transform","rotate("+deg+"deg)")
            deg++;
            if(deg == 360){
                deg = 0;
            }
        };
    },10)
    
    function getpushMessage(){
    	service.getPushMessage("",function(json){
        	if(json.code == "200" && json.list.length != 0){
        		var i = 0;
        		jpushMessage.hide(500);
        		jpushMessage.html(json.list[i].pushMessageContent);
        		jpushMessage.show(500);
        		var s1 = setInterval(function(){
        			i++;
        			if(i<json.list.length){
        				jpushMessage.hide(500);
            			jpushMessage.html(json.list[i].pushMessageContent);
            			jpushMessage.show(500);
        			}else{
        				clearInterval(s1);
        				getpushMessage();
        			}
        		},5000)
        	}
        });
    }
    getpushMessage();
    
    getCpuRate();
    setInterval(function(){
    	getCpuRate();
    },1000) 
    
    var cpuDeg = 0;
    var s;
    function getCpuRate(){
    	service.getCpuRate("",function(json){
    		clearInterval(s);
    		jmemWave.stop();
    		var runDeg = parseFloat(json.cpuRate)/100*180;
    		var runBtm = parseFloat(json.memRate)/100*55-54;
    		s = setInterval(function(){
    			if(cpuDeg > parseInt(runDeg)){
    				cpuDeg--;
    				jpointer.css("transform","rotate("+cpuDeg+"deg)")	
    			}
    			if(cpuDeg < parseInt(runDeg)){
    				cpuDeg++;
    				jpointer.css("transform","rotate("+cpuDeg+"deg)")
    			}
    			if(cpuDeg == parseInt(runDeg)){
    				clearInterval(s);
    				jpointer.css("transform","rotate("+cpuDeg+"deg)")
    			}
    		},10)
    		jmemWave.animate({bottom:runBtm+"px"},1000);
    	})
    }
    

    
    
})

/*
 * 定义一个环形统计图
 *@domId:初始化统计图dom Id
 *@data{
 *   value:统计数值
 }
 */
var ringChart = {
    random: function(l, u){
        return Math.floor((Math.random()*(u-l+1))+l);
    },
    diagram: function(domId,data){
        var r = Raphael(domId, 76, 76),
            rad = 5,
            defaultText = data.text,
            speed = 250;


        var title = r.text(38, 38, defaultText).attr({
            font: '14px 微软雅黑',
            fill: '#fff'
        }).toFront();

        r.customAttributes.arc = function(value, color, rad){
            var v = 3.6*value,
                alpha = v == 360 ? 359.99 : v,
                random = ringChart.random(90, 90),
                a = (random-alpha) * Math.PI/180,
                b = random * Math.PI/180,
                sx = 38 + rad * Math.cos(b),
                sy = 38 - rad * Math.sin(b),
                x = 38 + rad * Math.cos(a),
                y = 38 - rad * Math.sin(a),
                path = [['M', sx, sy], ['A', rad, rad, 0, +(alpha > 180), 1, x, y]];
            return { path: path, stroke: color }
        }

        var color = data.color,
            value = data.value,
            text = data.text;

        rad += 30;
        r.path().attr({ arc: [100, "#50A2EE", rad], 'stroke-width': 5 });
        var deg = 0;
        var z,c;
        var c = r.path().attr({ arc: [deg, color, rad], 'stroke-width': 5 });
        var s = setInterval(function(){
            c.remove();
            z = r.path().attr({ arc: [deg, color, rad], 'stroke-width': 5 });
            c = z;
            if(deg < value){
                deg++;
            }else{
                clearInterval(s);
            }
        },5)

        /*var z = r.path().attr({ arc: [value, color, rad], 'stroke-width': 10 });
        z.hover(function(){
            this.animate({ 'stroke-width': 20, opacity: .75 }, 1000, 'elastic');
            title.stop().animate({ opacity: 0 }, speed, '>', function(){
                this.attr({ text: text + '\n' + value + '%' }).animate({ opacity: 1 }, speed, '<');
            });
        },function(){
            this.stop().animate({ 'stroke-width': 10, opacity: 1 }, speed*4, 'elastic');
            title.stop().animate({ opacity: 0 }, speed, '>', function(){
                title.attr({ text: defaultText }).animate({ opacity: 1 }, speed, '<');
            });
        });*/

    }
}
